const axios = require('axios');
const MockAdapter = require('axios-mock-adapter');
const normalAxios = axios.create();
const mockAxios = axios.create();
import conf from "../utils/config";

// mock 数据
var mock = new MockAdapter(mockAxios);

mock.onGet('/getCaptcha').reply((config) => {
  return new Promise((resolve, reject) => {
    normalAxios.get(conf.primary + "/getCaptcha", config).then((res) => {
      resolve([res.status, res.data]);
    }).catch((err) => {
      reject(err);
    });
  });
});

mock.onPost('/validMailCode').reply((config) => {
  return new Promise((resolve, reject) => {
    config.url = conf.primary + "/validMailCode";
    config.headers = { 'Content-Type': 'application/json' }
    normalAxios(config).then((res) => {
      resolve([res.status, res.data]);
    }).catch((err) => {
      reject(err);
    });
  });
});

mock.onPut('/modifyPassword').reply((config) => {
  return new Promise((resolve, reject) => {
    config.url = conf.primary + "/modifyPassword";
    config.headers = { 'Content-Type': 'application/json' }
    normalAxios(config).then((res) => {
      resolve([res.status, res.data]);
    }).catch((err) => {
      reject(err);
    });
  });
});

mock.onPost('/sendMail').reply((config) => {
  return new Promise((resolve, reject) => {
    config.url = conf.primary + "/sendMail";
    config.headers = { 'Content-Type': 'application/json' };
    normalAxios(config).then((res) => {
      resolve([res.status, res.data]);
    }).catch((err) => {
      reject(err);
    });
  });
});

export default mockAxios;
